#include <project.h>
/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module OPT.c - Operation Parameter Table maintenance functions |
   @end

$Log: /SoftPay/SPK2SIM/Source/Config/opt.c $
 *
 * 4     2/21/00 2:59p Jim_b4
 * Include file restructuring
 *
 * 3     2/21/00 11:29a Jim_b4
 * Include directory restructuring
 *
 * 2     11/05/99 2:37p Kevin_v1
 * Modified inLoadOPTRec to search for match on inOperationCode instead of
 * using as an index.
 *
 * 1     7/19/99 5:32p Jim_b4
 *
 * 1     7/19/99 4:10p Jim_b4
 *
 * 5     7/13/99 5:48p Nick_t1
 *
 * 4     6/24/99 4:13p Nick_t1
 *
 * 3     6/10/99 11:45a Nick_t1
 *
 * 2     6/09/99 11:18a Monique_b1
 * Change headers and file names for SP 2000
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSOPT_C

#include <stdio.h>
#include <string.h>

#ifdef DEBUG_VSOPT
#define DEBUG_95
#endif

#include <define.h>
#include <confio.h>
#include <entry.h>
#include <opt.h>
#include <msg.h>
#include <spdebug.h>
#include <file.h>
#include <util.h>
#include <gds.h>

OPT_REC srOPTRec;

/*-------------------------------------------------------------------------
    Function :    inLoadOPTRec
    Description : Loads local buffer with contents of specified OPT.
    Parameters :
    Returns :     if successful SUCCESS
                   on error      ERR_FOPEN  - File open error
                                 ERR_FREAD  - File read error
                                 ERR_FCLOSE - File close error
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
     0   02/05/96  Henry_G1   Created.
     1   01/28/98  Kevin_V1   Modified to treat inOperation Code as an index
                              to the Operation Table.
--------------------------------------------------------------------------*/

int inLoadOPTRec(int inOperationCode) {
    int inRetVal = VS_ERR;
    int inResult;
    int inOPTRecNum = 0;  /* init index to first OPT record */
    FHANDLE hOPTFHandle;
    char stVerInfo[GEN_VER_SIZE];
    char szFileName[FILENAME_SIZE + 1];



    /** /pdebug (("--inLoadOPTRec--"));*/

    /*
     *      Open OPT Data File
     */
    strcpy(szFileName, OPT_FILE_NAME);
    //  if ( srGDS.fHandHeld )
    //        strcat(szFileName, ".dhh");
    //    else
    strcat(szFileName, ".dat");

    hOPTFHandle = open(szFileName, O_RDWR);

    if (hOPTFHandle > 0) {

        /*
             *      Skip GENDATA version header information
             */
        if (read(hOPTFHandle, stVerInfo, GEN_VER_SIZE) == GEN_VER_SIZE) {

            /*
                     *      Read OPT records and check for matching operation code
                     */
            do {

                inResult = read(hOPTFHandle, (char *) &srOPTRec, SIZE_OPT_REC);

                if (inResult == 0)
                    break;                           /* End of file reached */
                if (inResult != SIZE_OPT_REC)        /* or even result = -1 */ {

                    inRetVal = VS_ERR;
                    break;          /* Read error, get out */
                }
                if (srOPTRec.inOperationCode == inOperationCode) {

                    inRetVal = VS_SUCCESS;
                    break;
                }
                ++inOPTRecNum;
            } while (1);
        }
        close(hOPTFHandle);
    }

    return(inRetVal);
}

#undef VSOPT_C

